package com.qmerak.dao.impl;

import java.util.List;
import java.util.UUID;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.qmerak.dao.HibernateDao;
import com.qmerak.entity.MemberEntity;
import com.qmerak.entity.OrgEntity;
import com.qmerak.entity.PostUnitEntity;

/**
 * @author Shaohong•Cheng
 * @mail shaohong726@126.com
 * @date 2013-4-14
 * @project WebTest
 */
@Repository
public class MemberDao extends HibernateDao<MemberEntity, String> {
	public MemberEntity add(MemberEntity entity) {
		entity.setId(UUID.randomUUID().toString());
		return super.add(entity);
	}

	@SuppressWarnings("unchecked")
	public MemberEntity findByLoginName(String name) {
		Session session = this.sessionFactory.openSession();
		List<MemberEntity> list = session
				.createQuery("from MemberEntity where loginName=?")
				.setParameter(0, name).list();
		session.close();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings({ "unchecked", "unused" })
	private List<MemberEntity> memberOfOrg(OrgEntity orgEntity) {
		Session session = this.sessionFactory.openSession();
		List<MemberEntity> list = session
				.createQuery(
						"from MemberEntity where unionId=? and status<>? and status<>? and status<>?")
				.setParameter(0, orgEntity.getId())
				.setParameter(1, MemberEntity.Member_Status.LOGOFFED.name())
				.setParameter(2, MemberEntity.Member_Status.NORMAL.name())
				.setParameter(3, MemberEntity.Member_Status.LOGINING.name())
				.list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<MemberEntity> findMembers() {
		Session session = this.sessionFactory.openSession();
		List<MemberEntity> list = session
				.createQuery(
						"from MemberEntity where status<>? and status<>? and status<>?")
				.setParameter(0, MemberEntity.Member_Status.LOGOFFED.name())
				.setParameter(1, MemberEntity.Member_Status.NORMAL.name())
				.setParameter(2, MemberEntity.Member_Status.LOGINING.name())
				.list();
		session.close();
		return list;
	}

	public void updateRoles(MemberEntity entity) {
		MemberEntity oEntity = find(entity.getId());
		oEntity.setRoles(entity.getRoles());
		update(oEntity);
	}

	public void updateStatus(MemberEntity entity) {
		MemberEntity oEntity = find(entity.getId());
		oEntity.setStatus(entity.getStatus());
		update(oEntity);
	}

	@SuppressWarnings("unchecked")
	public List<MemberEntity> memberOfUnit(PostUnitEntity unitEntity,
			String condition) {
		String sql = "from MemberEntity where status<>? and status<>? and status<>? and workUnit=?";
		if (condition != null && !"".equals(condition)) {
			sql += " and " + condition;
		}
		Session session = this.sessionFactory.openSession();
		List<MemberEntity> list = session.createQuery(sql)
				.setParameter(0, MemberEntity.Member_Status.LOGOFFED.name())
				.setParameter(1, MemberEntity.Member_Status.NORMAL.name())
				.setParameter(2, MemberEntity.Member_Status.LOGINING.name())
				.setParameter(3, unitEntity.getId()).list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<MemberEntity> loginingMemberOfUnit(PostUnitEntity unitEntity) {
		Session session = this.sessionFactory.openSession();
		List<MemberEntity> list = session
				.createQuery(
						"from MemberEntity where status=?  and work_unit=?")
				.setParameter(0, MemberEntity.Member_Status.LOGINING.name())
				.setParameter(1, unitEntity.getId()).list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<MemberEntity> logoffingMemberOfUnit(PostUnitEntity unitEntity) {
		Session session = this.sessionFactory.openSession();
		List<MemberEntity> list = session
				.createQuery(
						"from MemberEntity where status=?  and work_unit=?")
				.setParameter(0, MemberEntity.Member_Status.LOGOFFING.name())
				.setParameter(1, unitEntity.getId()).list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<MemberEntity> unionChangingMemberOfUnit(
			PostUnitEntity unitEntity) {
		Session session = this.sessionFactory.openSession();
		List<MemberEntity> list = session
				.createQuery(
						"from MemberEntity where status=?  and work_unit=?")
				.setParameter(0,
						MemberEntity.Member_Status.UNIONCHANGING.name())
				.setParameter(1, unitEntity.getId()).list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<MemberEntity> findByIDNumber(MemberEntity memberEntity) {
		Session session = this.sessionFactory.openSession();
		List<MemberEntity> list = session
				.createQuery("from MemberEntity where ID_number=?")
				.setParameter(0, memberEntity.getIdNumber()).list();
		session.close();
		return list;
	}
}
